package com.aaa.mrcai.service.impl;

import cn.hutool.core.date.DateUtil;
import com.aaa.mrcai.mapper.OrderAddOneMapper;
import com.aaa.mrcai.pojo.entity.TestOne;
import com.aaa.mrcai.pojo.entity.UserInfo;
import com.aaa.mrcai.pojo.vo.ShopOneSoVO;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.OrderAddOneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;

/**
 * Demo class
 *
 * @Author: Wang
 * @Date: Created in 22:28 2021/12/21
 */
@Service
public class OrderAddServiceOneImpl implements OrderAddOneService {

    @Autowired
    private ServletContext servletContext;

    @Autowired
    private OrderAddOneMapper orderAddOneMapper;
    @Override
    public int addPendingOrderOne(TestOne testone) {
        //取出id看是否为正确的订单号
        String soid = testone.getSoid();
        System.out.println("OrderADDServiceImpl的soid:"+soid);

        //给test设置uid为当前登录的id
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        Integer uid = user.getId();
        testone.setUid(uid);
        System.out.println("uid:"+uid);

        //给test设置当前时间
        String today = DateUtil.now();
        testone.setOrderdate(today);
        System.out.println(today);
        int i = orderAddOneMapper.addPendingOrderOne(testone);
        Integer orderId = testone.getId();
        System.out.println("testone.getId()"+orderId);
        System.out.println("yttttttttt"+testone);
        servletContext.setAttribute("orderId",orderId);

//        System.out.println("shopOneVOAAAAaa"+shopOneSoVO);
//        orderAddOneMapper.addOneGoodInOrderOne(shopOneSoVO);
        return i;
    }

    @Override
    public int addOneGoodInOrderOne(ShopOneSoVO shopOneSoVO) {
        Integer  oId = (Integer) servletContext.getAttribute("orderId");
        System.out.println("ervletContext.getAttribute(\"ord"+oId);
        shopOneSoVO.setOid(oId);
        System.out.println("shopOneSoVO"+shopOneSoVO);
        int i = orderAddOneMapper.addOneGoodInOrderOne(shopOneSoVO);
        return i;
    }

    @Override
    public int updataOrder(TestOne liushui) {
        return orderAddOneMapper.updataOrder(liushui);
    }

//    @Override
//    public AjaxResult inventory(ShopOneSoVO shopOneSoVO) {
//        long inventory = orderAddOneMapper.inventory(shopOneSoVO);
//        if(inventory<shopOneSoVO.getNum()){
//            return AjaxResult.defeated("您购买的数量太多了哟");
//        }
//        return AjaxResult.success("可以购买");
//    }
}
